package com.ycfmedical.bed.utils;

import com.ycfmedical.bed.Constants;
import com.ycfmedical.bed.wifi.ClientThread;
import com.ycfmedical.bed.wifi.WifiEvent;

/**
 * 自定义进制转换,指令校验工具类,网络状态校验工具类
 * Created by Administrator on 2017-03-27.
 */

public class ConvertOrderCheckoutUtil{

    //10进制int转16进制String
    public static String integerC10T16(int number)
    {
        String num = Integer.toHexString(number);
        return num;
    }

    //16进制String转10进制int
    public static int StrC16T10(String number){

        int num =  Integer.valueOf(number,
                16);
        return num;
    }

    //指令校验()
    public static String instructCheckOut(String instruct) {
        if(instruct.length() - Constants.wrongOrderMsg.length() != 0 )
        {
            return Constants.wrongOrderMsg;
        }
        else
        {
            return instruct;
        }
//        //当前不需要做指令校验
//        String operationOrder = instruct.substring(8, 44);//截取后的操作指令
//        int OrderSum = 0;//操作指令之和(int)
//        int OrderNum = 0;//当前角标指令数值(十进制)
//        String OrderSumStr = "";//操作指令之和(String)未补0
//        String OrderSumFinsih = "";//操作指令之和(String)未补0
//        for (int i = 0; i < (operationOrder.length() / 2); i++)
//        {
//            OrderNum = StrC16T10(operationOrder.substring(2 * i, 2 + 2 * i));
//            OrderSum = OrderSum+OrderNum;
//        }
//        OrderSumStr = integerC10T16(OrderSum);
//        //补零操作
//        switch (OrderSumStr.length()){
//            case 1:
//                OrderSumFinsih = "0"+OrderSumStr;
//                break;
//            case 2:
//                OrderSumFinsih = OrderSumStr;
//                break;
//            case 3:
//                OrderSumFinsih = OrderSumStr.substring(1,3);
//                break;
//            case 4:
//                OrderSumFinsih = OrderSumStr.substring(2,4);
//                break;
//            default:
//                break;
//        }
//        String Checked = instruct + OrderSumFinsih;
//        return Checked;
    }

    //倾斜角度计算
    public static String calculateAngle(String orderMsg,int Angle) {

        String FH = orderMsg.substring(0, 16);//帧头
        String endFrame = orderMsg.substring(18, 46);//帧尾
        String CountAngleStr = String.valueOf(integerC10T16(Angle)+15);//角度参数未补零
        //补零操作
        if(CountAngleStr.length() == 1){
            CountAngleStr = "0"+CountAngleStr;
        }
        String Checked = FH+CountAngleStr+endFrame;
        return Checked;
    }

    //床体高度计算
    public static String calculateBedHeight(String orderMsg,int bedHeight) {
        String FH = orderMsg.substring(0, 16);//帧头
        String endFrame = orderMsg.substring(18, 46);//帧尾
        String CountHeightStr = "";//床体高度参数未补0
        CountHeightStr = String.valueOf(integerC10T16(bedHeight));
        //补零操作
        if(CountHeightStr.length() == 1){
            CountHeightStr = "0"+CountHeightStr;
        }
        String Checked = FH+CountHeightStr+endFrame;
        return Checked;
    }

    // 超长ASCII转换成字符串
    public static String asciiToLongString(String value)
    {
        String OrderCount = "";//
        String OrderMsg = "";
        String OrderMsgCheck = "";
        for (int i = 0; i < (value.length() / 2); i++)
        {
            OrderCount = asciiToString(value.substring(2 * i, 2 + 2 * i));
            if (!OrderCount.equals("\r") && !OrderCount.equals("\n"))
            {
                OrderMsg = OrderMsg + OrderCount;
            }
        }
        try
        {
            OrderMsgCheck = (OrderMsg.replace(" ", "")).substring(0, 46);
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
        return OrderMsgCheck;
    }
    
    // ASCII转换成字符串
    public static String asciiToString(String value)
    {
        StringBuffer sbu = new StringBuffer();
        String[] chars = value.split(",");
        for (int i = 0; i < chars.length; i++)
        {
            sbu.append((char)Integer.parseInt(chars[i], 16));
        }
        return sbu.toString();
    }

    // 带小数的字符串转化为double

    public static int decimalToDouble(String value)
    {
        java.text.DecimalFormat myformat=new java.text.DecimalFormat("#0.00");
        Number ValueF=Float.parseFloat(value) * 100;;//装换为double类型
        int num = ValueF.intValue();
        return num;
    }

    // 角度指令转化为一键体位指令

    public static String angleMsgChangeQuicklyMsg(int postId,String angleMsg)
    {
        String quicklyMsg = "";
        String postNum = "";

        if (postId ==1 )
        {
            postNum = "1A";
        }
        else if (postId ==2)
        {
            postNum = "2B";
        }
        else if (postId ==3)
        {
            postNum = "3C";
        }
        else if (postId ==4)
        {
            postNum = "4D";
        }

        //头板角度
        String headAngle = angleMsg.substring(18,20);
        //腿板角度
        String legAngle = angleMsg.substring(30,32);
        //整床倾斜角度
        String slantAngle = angleMsg.substring(16,18);
        //整床高度
        String bedHeight = angleMsg.substring(12,16);
        quicklyMsg = Constants.fhHeader+Constants.zgbId+"0003"+postNum+headAngle+"0000000000"
                +legAngle+"0000000000"+slantAngle+bedHeight+"00";
        return quicklyMsg;
    }



}
